import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.JButton;


public class Main extends JFrame
{
	private JButton  btn_BinarySearch;
	private JButton  btn_Matrix;
	private JButton btn_BubbleSort;
	private JButton btn_StackQueue;
	private ClosableTabbedPane av_TabFrame;
	
	// these counters are printed in the tab title when new tabs are created
	private int bubblesort_tab_count;
	private int binarytree_tab_count;
	private int matrix_tab_count;
	private int stack_tab_count;

	public static void main(String[] args)
	{
		
	    try {
		    // Set cross-platform Java L&F (also called "Metal")
	        UIManager.setLookAndFeel(
	            UIManager.getSystemLookAndFeelClassName());
	    } 
	    catch (UnsupportedLookAndFeelException e) {
	       // handle exception
	    } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		Main window = new Main();
		
	}

	public Main()
	{
		
		// initialize tab counters to zero
		bubblesort_tab_count = 1;
		binarytree_tab_count = 1;
		matrix_tab_count = 1;
		stack_tab_count = 1;
		
		this.setSize(new Dimension(1100,600));
		this.setTitle("AlgoVision Algorithm Visualization Tool");
		
		// inside our Main window container, add a new panel that will hold the main layout
		// which is the menu bar, buttons, and tabbed frames
		JPanel MainFrame = new JPanel();
		MainFrame.setLayout(new BorderLayout());
		MainFrame.add(TopPanel(), BorderLayout.NORTH);
		MainFrame.setVisible(true);

		av_TabFrame = new ClosableTabbedPane();
		MainFrame.add(av_TabFrame, BorderLayout.CENTER);
	
		this.setContentPane(MainFrame);	
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	
	}
	
	// contains the menu bar and the button bar, maybe separate into two?
	private JPanel TopPanel()
	{
		
		

		// create the buttons to allow the user to change between functionalities
		btn_BinarySearch = new JButton();
		btn_Matrix = new JButton();
		btn_StackQueue = new JButton();
		btn_BubbleSort = new JButton();
		
		btn_BinarySearch.setText("Binary Trees");
		btn_Matrix.setText("Matrices");
		btn_StackQueue.setText("Stacks and Queues");
		btn_BubbleSort.setText("Bubble Sort");
		
		//add the button listeners
		btn_BinarySearch.addActionListener(new initialButtonListener());
		btn_Matrix.addActionListener(new initialButtonListener());
		btn_StackQueue.addActionListener(new initialButtonListener());
		btn_BubbleSort.addActionListener(new initialButtonListener());
		
		
		// add the buttons to their own panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(btn_BinarySearch);
		buttonPanel.add(btn_Matrix);
		buttonPanel.add(btn_StackQueue);
		buttonPanel.add(btn_BubbleSort);
		
		// the menu bar File, Help, etc.
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem exitItem = new JMenuItem("Exit");
		menuBar.add(fileMenu);
		fileMenu.add(exitItem);
		
		// add the button panel and the menu bar to one panel
		JPanel menuPanel = new JPanel();
		menuPanel.setLayout(new BorderLayout());
		menuPanel.add(menuBar, BorderLayout.NORTH);
		menuPanel.add(buttonPanel, BorderLayout.CENTER);

		return menuPanel;
	}

	private class initialButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
			if (e.getSource()== btn_BinarySearch) 
			{
				av_TabFrame.add("BinaryTree" + binarytree_tab_count, new BinaryTreePanel());
				binarytree_tab_count++;
			}
			else if (e.getSource()== btn_Matrix)
			{
				av_TabFrame.add("Matrix" + matrix_tab_count, new MatrixPanel());
				matrix_tab_count++;
			}
			else if (e.getSource()==btn_StackQueue)
			{
				av_TabFrame.add("StackQueue" + stack_tab_count, new StackQueuePanel());
				stack_tab_count++;
			}
			else if (e.getSource()==btn_BubbleSort)
			{
				av_TabFrame.add("BubbleSort" + bubblesort_tab_count, new BubbleSortPanel());
				bubblesort_tab_count++;
			}

		}
		
		
		
		
		
	}
	
	
	



}

